package com.example.star.view.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import android.os.Bundle;
import android.view.MenuItem;

import com.example.star.R;
import com.example.star.view.fragment.PublishFragment;
import com.example.star.view.fragment.HomeFragment;
import com.example.star.view.fragment.PostFragment;
import com.example.star.view.fragment.CenterFragment;
import com.example.star.view.fragment.PerimeterFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化底部导航栏
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(navListener);

        // 默认显示首页 Fragment
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
    }

    private BottomNavigationView.OnNavigationItemSelectedListener navListener =
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    Fragment selectedFragment = null;
                    int itemId = item.getItemId();

                    if (itemId == R.id.navigation_home) {
                        selectedFragment = new HomeFragment();
                    } else if (itemId == R.id.navigation_notifications) {
                        selectedFragment = new PostFragment();
                    } else if (itemId == R.id.navigation_dashboard) {
                        selectedFragment = new PublishFragment();
                    } else if (itemId == R.id.navigation_settings) {
                        selectedFragment = new PerimeterFragment();
                    }else if (itemId == R.id.navigation_profile) {
                        selectedFragment = new CenterFragment();
                    }

                    // 替换当前显示的 Fragment
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment).commit();

                    return true;
                }
            };
}